package com.htandroid.htandroid.aty;

/**
 * Created by Administrator on 2015/12/26.
 */

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.fragment.AbDialogFragment;
import com.ab.fragment.AbLoadDialogFragment;
import com.ab.model.AbMenuItem;
import com.ab.soap.AbSoapUtil;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.ab.view.ioc.AbIocView;
import com.ab.view.titlebar.AbTitleBar;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.adapter.ListPopAdapter;
import com.htandroid.htandroid.entity.GcwxList;
import com.htandroid.htandroid.entity.GcwxVouchs;
import com.htandroid.htandroid.entity.GcwxVouchsForeign;
import com.htandroid.htandroid.entity.GcwxVouchsPersons;
import com.htandroid.htandroid.global.MyApplication;
import com.htandroid.htandroid.sys.Basenvo;
import com.htandroid.htandroid.sys.DateTimePickDialogUtil;
import com.htandroid.htandroid.sys.InstorageURL;
import com.htandroid.htandroid.sys.User;
import com.htandroid.htandroid.webservice.BuildGcwxXMLService;
import com.htandroid.htandroid.webservice.XMLGcwxVouchlist;
import com.htandroid.htandroid.webservice.XMLGcwxVouchsForeignlist;
import com.htandroid.htandroid.webservice.XMLGcwxVouchsPersonslist;
import com.htandroid.htandroid.webservice.XMLGcwxVouchslist;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.htandroid.htandroid.constance.ConstanceServer.BaseUrl_New;

public class Gcwx_xd_detail extends AbActivity {
    public MyApplication myApplication;
    private GcwxList vouch;
    private ArrayList<GcwxList> vouchlist;
    private ArrayList<GcwxVouchs> vouchslist;
    private ArrayList<GcwxVouchsForeign> vouchsforeignlist;
    private ArrayList<GcwxVouchsPersons> vouchspersonslist;
    private String initTime = "";
    private String listtype = "0";
    private int icurrentrow;

    private AbTitleBar mAbTitleBar = null;
    private View rightView1 = null;
    private View rightView2 = null;
    private View rightView3 = null;
    private TextView tv_currentrow;

    private AbLoadDialogFragment mDialogFragment = null;
    private PopupWindow popupWindow;
    private ListView popListView;
    private View popView;

    @AbIocView(id = R.id.dwcdate_et)
    EditText dwcdate_et;
    @AbIocView(id = R.id.cmemo_et)
    EditText cmemo_et;
    @AbIocView(id = R.id.cxder_et)
    TextView cxder_et;
    @AbIocView(id = R.id.dxddate_et)
    TextView dxddate_et;
    @AbIocView(id = R.id.cvouchid_tv)
    TextView cvouchid_tv;
    @AbIocView(id = R.id.dwcdate_iv)
    ImageView dwcdate_iv;
    @AbIocView(id = R.id.btn1_tv, click = "btnClick")
    TextView btn1_tv;
    @AbIocView(id = R.id.btn2_tv, click = "btnClick")
    TextView btn2_tv;
    @AbIocView(id = R.id.btn3_tv, click = "btnClick")
    TextView btn3_tv;
    @AbIocView(id = R.id.btn4_tv, click = "btnClick")
    TextView btn4_tv;
    @AbIocView(id = R.id.btn5_tv, click = "btnClick")
    TextView btn5_tv;
    @AbIocView(id = R.id.btn6_tv, click = "btnClick")
    TextView btn6_tv;
    @AbIocView(id = R.id.cdepname_tv)
    TextView cdepname_tv;
    @AbIocView(id = R.id.cbxtext_tv)
    TextView cbxtext_tv;
    @AbIocView(id = R.id.ddate_tv)
    TextView ddate_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.layout_gcwx_xd_detail);
        initview();
        getdata();
//        mDialogFragment = AbDialogUtil.showLoadDialog(this, R.layout.view_dialog_loading, "正在加载...");
//        mDialogFragment.setAbDialogOnLoadListener(new AbDialogFragment.AbDialogOnLoadListener() {
//
//            @Override
//            public void onLoad() {
//                getdata();
//            }
//        });
    }

    public void btnClick(final View v) {
        switch (v.getId()) {
            //功能按钮监听,销单
            case R.id.btn1_tv:
                mDialogFragment = AbDialogUtil.showLoadDialog(this, R.layout.view_dialog_loading, "正在加载...");
                mDialogFragment.setAbDialogOnLoadListener(new AbDialogFragment.AbDialogOnLoadListener() {

                    @Override
                    public void onLoad() {
                        btn1_click(v);
                    }
                });
                break;
            //功能按钮监听,取消
            case R.id.btn2_tv:
                mDialogFragment = AbDialogUtil.showLoadDialog(this, R.layout.view_dialog_loading, "正在加载...");
                mDialogFragment.setAbDialogOnLoadListener(new AbDialogFragment.AbDialogOnLoadListener() {

                    @Override
                    public void onLoad() {
                        btn2_click(v);
                    }
                });
                break;
            //功能按钮监听,延缓
            case R.id.btn3_tv:
                btn3_click(v);
                break;
            //功能按钮监听,耗用材料
            case R.id.btn4_tv:
                btn4_click(v);
                break;
            //功能按钮监听,外包材料
            case R.id.btn5_tv:
                btn5_click(v);
                break;
            //功能按钮监听,维修工时
            case R.id.btn6_tv:
                btn6_click(v);
                break;
        }
    }

    //初始化
    private void initview() {
        myApplication = (MyApplication) abApplication;//获得我们的应用程序全局变量
        mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("维修销单");
        mAbTitleBar.setLogo(R.drawable.icon_back);
        mAbTitleBar.getLogoView().setLayoutParams(new LinearLayout.LayoutParams(58, 57));
        mAbTitleBar.getLogoView().setFocusable(true);
        mAbTitleBar.getLogoView().setFocusableInTouchMode(true);
        mAbTitleBar.getLogoView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("currentrow", icurrentrow);//把当前行返回到上一页面,方便更换上一页面的选中行
                setResult(R.layout.basenvo_nofunc / 300000, intent);  //设置返回结果
                //返回时增加提示,防止误点
                finish();
            }
        });
        mAbTitleBar.setTitleBarBackground(R.color.common_title);
        mAbTitleBar.setTitlePadding(118, 0, 0, 0);

        mAbTitleBar.clearRightView();
        rightView1 = mInflater.inflate(R.layout.btn_abs_back, null);
        mAbTitleBar.addRightView(rightView1);
        rightView2 = mInflater.inflate(R.layout.tv_currentrow, null);
        mAbTitleBar.addRightView(rightView2);
        rightView3 = mInflater.inflate(R.layout.btn_abs_forward, null);
        mAbTitleBar.addRightView(rightView3);
        tv_currentrow = (TextView) rightView2.findViewById(R.id.tv_currentrow);

        //获取传递的对象参数
        Intent intent = getIntent();
        vouchlist = (ArrayList<GcwxList>) intent.getSerializableExtra("vouchlist");
        listtype = intent.getExtras().getString("listtype");
        icurrentrow = getIntent().getExtras().getInt("currentrow");

        insert_data(icurrentrow);

        //弹出窗口
        popView = mInflater.inflate(R.layout.list_pop, null);
        popListView = (ListView) popView.findViewById(R.id.pop_list);
        popupWindow = new PopupWindow(popView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        AnimationSet animationSet = new AnimationSet(true);
        ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 1, 1, Animation.ZORDER_BOTTOM, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animationSet.setDuration(250);
        animationSet.addAnimation(scaleAnimation);
        popView.startAnimation(animationSet);
        ImageView more_close = (ImageView) popView.findViewById(R.id.item_close);
        more_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
    }

    public void getdata() {
        //获取维修使用材料
        String urlString = "http://" + InstorageURL.SERVERURL
                + BaseUrl_New;
        String[] parms = {"cacc_id", "iyear", "tablename", "cvouchid"};
        String[] vals = {myApplication.getGs_ztid(), myApplication.getGs_year(), "HTWX_Vouchs", vouch.getCvouchid()};
        //生成xml
        BuildGcwxXMLService service = new BuildGcwxXMLService();
        String xmlstring = service.buildXML("gcwx_vouchslist", InstorageURL.NAME_SPACE, parms, vals, null, null, null, null, null, null, null, null);
        mAbSoapUtil.xmlCall(urlString, InstorageURL.NAME_SPACE, "gcwx_vouchslist", xmlstring, new AbSoapUtil.WebServiceCallBack() {
            @Override
            public void onSucced(String result) {
                if (result != null) {
                    //解析xml文件,返回列表
                    vouchslist = XMLGcwxVouchslist.paraseCommentInfors(result);
                } else {
                    AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
                }
                AbDialogUtil.removeDialog(Gcwx_xd_detail.this);
            }

            @Override
            public void onFailure(String result) {
                AbDialogUtil.removeDialog(Gcwx_xd_detail.this);
                AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
            }
        });

        //获取维修外包材料
        //生成xml
        xmlstring = service.buildXML("gcwx_vouchsforeignlist", InstorageURL.NAME_SPACE, parms, vals, null, null, null, null, null, null, null, null);
        mAbSoapUtil.xmlCall(urlString, InstorageURL.NAME_SPACE, "gcwx_vouchsforeignlist", xmlstring, new AbSoapUtil.WebServiceCallBack() {
            @Override
            public void onSucced(String result) {
                if (result != null) {
                    //解析xml文件,返回列表
                    vouchsforeignlist = XMLGcwxVouchsForeignlist.paraseCommentInfors(result);
                } else {
                    AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
                }
                AbDialogUtil.removeDialog(Gcwx_xd_detail.this);
            }

            @Override
            public void onFailure(String result) {
                AbDialogUtil.removeDialog(Gcwx_xd_detail.this);
                AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
            }
        });

        //获取维修工时
        //生成xml
        xmlstring = service.buildXML("gcwx_vouchspersonslist", InstorageURL.NAME_SPACE, parms, vals, null, null, null, null, null, null, null, null);
        mAbSoapUtil.xmlCall(urlString, InstorageURL.NAME_SPACE, "gcwx_vouchspersonslist", xmlstring, new AbSoapUtil.WebServiceCallBack() {
            @Override
            public void onSucced(String result) {
                if (result != null) {
                    //解析xml文件,返回列表
                    vouchspersonslist = XMLGcwxVouchsPersonslist.paraseCommentInfors(result);
                    if ((vouchspersonslist == null || vouchspersonslist.size() < 1) && listtype.equals("0")) {
                        //如果没有获取，则重新生成，维修员的工时列表
                        GcwxVouchsPersons mvouchspersons = new GcwxVouchsPersons();
                        //主维修人
                        mvouchspersons.setCvouchid(vouch.getCvouchid());
                        mvouchspersons.setCwxpcode(vouch.getCwxpcode());
                        mvouchspersons.setCwxpname(vouch.getCwxpname());
                        mvouchspersons.setCwxptype("1");
                        mvouchspersons.setCwxptypename("主维修人");
                        //工分
                        vouchspersonslist.add(mvouchspersons);
                        //协助维修人
                        if (vouch.getXzwxcodes() != null && !vouch.getXzwxcodes().equals("")) {
                            String[] xzwxcodes = vouch.getXzwxcodes().split("，");
                            String[] xzwxnames = vouch.getXzwxnames().split("，");
                            for (int i = 0; i < xzwxcodes.length; i++) {
                                GcwxVouchsPersons xzvouchspersons = new GcwxVouchsPersons();
                                xzvouchspersons.setCvouchid(vouch.getCvouchid());
                                xzvouchspersons.setCwxpcode(xzwxcodes[i]);
                                xzvouchspersons.setCwxpname(xzwxnames[i]);
                                xzvouchspersons.setCwxptype("2");
                                xzvouchspersons.setCwxptypename("协助维修人");
                                //工分
                                vouchspersonslist.add(xzvouchspersons);
                            }
                        }
                    }
                } else {
                    AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
                }
                AbDialogUtil.removeDialog(Gcwx_xd_detail.this);
            }

            @Override
            public void onFailure(String result) {
                AbDialogUtil.removeDialog(Gcwx_xd_detail.this);
                AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
            }
        });
    }

    //为窗口填充数据
    public void insert_data(int row) {
        vouch = vouchlist.get(row);
        icurrentrow = row;
        tv_currentrow.setText((icurrentrow + 1) + "/" + (vouchlist.size()));
        //接单和取消接单
        if (listtype.equals("0")) {
            showwin(true);
            //隐藏取消接单
            btn2_tv.setVisibility(View.GONE);
        } else {
            showwin(false);
            //隐藏接单和延缓
            btn1_tv.setVisibility(View.GONE);
            btn3_tv.setVisibility(View.GONE);
        }
        //填写文本内容
        cvouchid_tv.setText(vouch.getCvouchid());

        if (!vouch.getCdepcode().equals("")) {
            cdepname_tv.setText(vouch.getCdepname());
            cdepname_tv.setTag(vouch.getCdepcode());
        }

        cbxtext_tv.setText(vouch.getCbxtext());
        ddate_tv.setText(Basenvo.customDateFormat("yyyy-MM-dd HH:mm", vouch.getDdate()));

        Date curDate = new Date(System.currentTimeMillis());
        //完成时间
        if (!vouch.getDwcdate().equals("")) {
            initTime = Basenvo.customDateFormat("yyyy-MM-dd HH:mm", vouch.getDwcdate());
        } else {
            initTime = Basenvo.customDateFormat("yyyy-MM-dd HH:mm", curDate);
        }
        dwcdate_et.setText(initTime);
        //销单时间
        if (!vouch.getDxddate().equals("")) {
            initTime = Basenvo.customDateFormat("yyyy-MM-dd HH:mm", vouch.getDxddate());
        } else {
            initTime = Basenvo.customDateFormat("yyyy-MM-dd HH:mm", curDate);
        }
        dxddate_et.setText(initTime);
        //
        if (!Basenvo.equal(vouch.getCxder(), "")) {
            cxder_et.setText(vouch.getCxder());
        } else {
            cxder_et.setText("");
        }
        //结论
        if (!Basenvo.equal(vouch.getCmemo(), "")) {
            cmemo_et.setText(vouch.getCmemo());
        } else {
            cmemo_et.setText("");
        }
        //写入表头数据
        insert_data_title();
    }

    public void insert_data_title() {
        //表头信息显示弹窗
        ImageView listmore_iv = (ImageView) findViewById(R.id.titlemore_iv);
        listmore_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<Map<AbMenuItem, AbMenuItem>> data = new ArrayList<Map<AbMenuItem, AbMenuItem>>();
                final ImageView imgs = (ImageView) v;
                Map<AbMenuItem, AbMenuItem> map1 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map2 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map3 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map4 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map5 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map6 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map7 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map8 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map9 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map10 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map11 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map12 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map13 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map14 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map15 = new HashMap<AbMenuItem, AbMenuItem>();
                map1.put(new AbMenuItem("　　报修区域："), new AbMenuItem(vouch.getCwxplname()));
                map2.put(new AbMenuItem("　　报修等级："), new AbMenuItem(vouch.getCdjname()));
                map3.put(new AbMenuItem("　　　　状态："), new AbMenuItem(vouch.getCstatus()));
                map4.put(new AbMenuItem("　　报修类型："), new AbMenuItem(vouch.getCtypename()));
                map5.put(new AbMenuItem("　　维修类别："), new AbMenuItem(""));
                map6.put(new AbMenuItem("　　维修工分："), new AbMenuItem(""));
                map7.put(new AbMenuItem("　　　报修人："), new AbMenuItem(vouch.getCmaker()));
                map8.put(new AbMenuItem("预计维修时间："), new AbMenuItem(Basenvo.customDateFormat("yyyy-MM-dd HH:mm", vouch.getEswxdate())));
                map9.put(new AbMenuItem("　　　接单人："), new AbMenuItem(vouch.getCjder()));
                map10.put(new AbMenuItem("　　　维修组："), new AbMenuItem(vouch.getCwxgcode()));
                map11.put(new AbMenuItem("　　　维修人："), new AbMenuItem(vouch.getCwxpcode()));
                map12.put(new AbMenuItem("　    协助维修人："), new AbMenuItem(vouch.getXzwxnames()));
                data.add(map1);
                data.add(map2);
                data.add(map3);
                data.add(map4);
                data.add(map5);
                data.add(map6);
                data.add(map7);
                data.add(map8);
                data.add(map9);
                data.add(map10);
                data.add(map11);
                data.add(map12);
                if (!Basenvo.equal(vouch.getCsuspender(), "")) {
                    map13.put(new AbMenuItem("　　　延缓人："), new AbMenuItem(vouch.getCsuspender()));
                    map14.put(new AbMenuItem("　　延缓时间："), new AbMenuItem(vouch.getDsuspenddate()));
                    map15.put(new AbMenuItem("　　延缓说明："), new AbMenuItem(vouch.getCsuspendreason()));
                    data.add(map13);
                    data.add(map14);
                    data.add(map15);
                }
                ListPopAdapter mListPopAdapter = new ListPopAdapter(Gcwx_xd_detail.this, data, R.layout.list_popitem);
                popListView.setAdapter(mListPopAdapter);
                //弹出窗口后,变更更多图标
                imgs.setImageResource(R.drawable.item_more_choose);
                popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    public void onDismiss() {
                        //还原图标
                        imgs.setImageResource(R.drawable.item_more);
                    }
                });
                popupWindow.showAsDropDown(v);
            }
        });
    }

    //选择时间
    public void date_choose(View v) {
        initTime = dwcdate_et.getText().toString();
        DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
                Gcwx_xd_detail.this, initTime);
        dateTimePicKDialog.dateTimePicKDialog(dwcdate_et);
    }

    public void showwin(boolean stype) {
        //背景变白，字变黑色
        Integer backgroundcolor = Color.TRANSPARENT;
        cmemo_et.setEnabled(stype);
        if (stype) {
            dwcdate_iv.setVisibility(View.VISIBLE);
            dwcdate_et.setBackgroundResource(R.drawable.edit_background);
            cmemo_et.setBackgroundResource(R.drawable.edit_background);
        } else {
            dwcdate_iv.setVisibility(View.GONE);
            dwcdate_et.setBackgroundColor(backgroundcolor);
            cmemo_et.setBackgroundColor(backgroundcolor);
        }
    }

    //维修信息保存
    public void btn1_click(View v) {
        if (cmemo_et.getText() == null || cmemo_et.getText().toString().equals("")) {
            AbDialogUtil.removeDialog(Gcwx_xd_detail.this);
            AbToastUtil.showToastInThread(getApplicationContext(), "必须输入销单结论!");
            return;
        }
        //写入备注
        vouch.setCmemo(cmemo_et.getText().toString());

        //写入完成时间
        vouch.setDwcdate(dwcdate_et.getText().toString());
        //如果没有列表这里还需要默认添加一个空列表作为参数
        if (vouchslist == null) {
            GcwxVouchs vouchs = new GcwxVouchs();
            vouchslist.add(vouchs);
        }

        if (vouchsforeignlist == null) {
            GcwxVouchsForeign vouchsforeign = new GcwxVouchsForeign();
            vouchsforeignlist.add(vouchsforeign);
        }

        if (vouchspersonslist == null) {
            GcwxVouchsPersons vouchspersons = new GcwxVouchsPersons();
            vouchspersonslist.add(vouchspersons);
        }

        String urlString = "http://" + InstorageURL.SERVERURL
                + BaseUrl_New;
        String[] parms = {"username", "cacc_id", "iyear"};
        String[] vals = {User.nickName, (String) myApplication.getGs_ztid(), (String) myApplication.getGs_year()};
        //生成xml
        BuildGcwxXMLService service = new BuildGcwxXMLService();
        String xmlstring = service.buildXML("gcwx_xdsave", InstorageURL.NAME_SPACE, parms, vals, "in" + String.valueOf(parms.length), vouch, "in" + String.valueOf(parms.length + 1), vouchslist, "in" + String.valueOf(parms.length + 2), vouchsforeignlist, "in" + String.valueOf(parms.length + 3), vouchspersonslist);
        mAbSoapUtil.xmlCall(urlString, InstorageURL.NAME_SPACE, "gcwx_xdsave", xmlstring, new AbSoapUtil.WebServiceCallBack() {
            @Override
            public void onSucced(String result) {
                if (result != null) {
                    //解析xml文件,返回列表
                    List<GcwxList> vouchlist = XMLGcwxVouchlist.paraseCommentInfors(result);
                    //默认显示第一行数据
                    if (vouchlist.size() > 0) {
                        vouch = vouchlist.get(0);
                    }
                    if (vouch.getSucstatus().equals("0")) {
                        AbToastUtil.showToastInThread(getApplicationContext(), "单据保存失败!");
                    } else if (vouch.getSucstatus().equals("-101")) {
                        AbToastUtil.showToastInThread(getApplicationContext(), "单据已经审核,不能销单!");
                    } else {
                        Intent intent = new Intent();
                        setResult(R.layout.layout_gcwx_xd_detail / 100000, intent);  //设置返回结果
                        finish();
                    }
                } else {
                    AbToastUtil.showToastInThread(getApplicationContext(), "单据保存失败!数据库错误!");
                }
                AbDialogUtil.removeDialog(Gcwx_xd_detail.this);
            }

            @Override
            public void onFailure(String result) {
                AbDialogUtil.removeDialog(Gcwx_xd_detail.this);
            }
        });
    }

    //取消销单
    public void btn2_click(View v) {
        //已审单据不允许取消
        if (!Basenvo.equal(vouch.getCchecker(), "")) {
            AbToastUtil.showToastInThread(getApplicationContext(), "单据已经审核,不能取消!");
            return;
        }
        String urlString = "http://" + InstorageURL.SERVERURL
                + BaseUrl_New;
        String[] parms = {"username", "cacc_id", "iyear", "ctype"};
        String[] vals = {User.nickName, (String) myApplication.getGs_ztid(), (String) myApplication.getGs_year(), "xd"};
        //生成xml
        BuildGcwxXMLService service = new BuildGcwxXMLService();
        String xmlstring = service.buildXML("gcwx_qx", InstorageURL.NAME_SPACE, parms, vals, "in" + String.valueOf(parms.length), vouch, null, null, null, null, null, null);
        mAbSoapUtil.xmlCall(urlString, InstorageURL.NAME_SPACE, "gcwx_qx", xmlstring, new AbSoapUtil.WebServiceCallBack() {
            @Override
            public void onSucced(String result) {
                if (result != null) {
                    //解析xml文件,返回列表
                    List<GcwxList> vouchlist = XMLGcwxVouchlist.paraseCommentInfors(result);
                    //默认显示第一行数据
                    if (vouchlist.size() > 0) {
                        vouch = vouchlist.get(0);
                    }
                    if (vouch.getSucstatus().equals("0")) {
                        AbToastUtil.showToastInThread(getApplicationContext(), "单据保存失败!");
                    } else {
                        Intent intent = new Intent();
                        setResult(R.layout.layout_gcwx_xd_detail / 100000, intent);  //设置返回结果
                        finish();
                    }
                } else {
                    AbToastUtil.showToastInThread(getApplicationContext(), "单据删除失败!数据库错误");
                }
                AbDialogUtil.removeDialog(Gcwx_xd_detail.this);
            }

            @Override
            public void onFailure(String result) {
                AbDialogUtil.removeDialog(Gcwx_xd_detail.this);
                AbToastUtil.showToastInThread(getApplicationContext(), "单据删除失败!数据库错误");
            }
        });
    }

    //延缓
    public void btn3_click(View v) {
        Intent intent = new Intent(getApplicationContext(), Gcwx_yh_detail.class);
        //传递参数
        Bundle bundle = new Bundle();
        bundle.putSerializable("vouchlist", vouchlist);
        intent.putExtra("listtype", listtype);
        intent.putExtra("currentrow", icurrentrow);//传递当前行,下一界面便于划动切换
        intent.putExtras(bundle);
        startActivityForResult(intent, R.layout.layout_gcwx_yh_detail / 500000);
        //打开延缓窗口,关闭当前窗口
        this.finish();
    }

    //耗用材料
    public void btn4_click(View v) {
        Intent intent = new Intent(getApplicationContext(), Gcwx_inventory.class);
        //传递参数
        Bundle bundle = new Bundle();
        bundle.putSerializable("vouchslist", vouchslist);
        bundle.putSerializable("vouch", vouch);
        intent.putExtra("listtype", listtype);
        intent.putExtra("ctype", "xd");
        intent.putExtra("currentrow", 0);
        intent.putExtras(bundle);
        startActivityForResult(intent, R.layout.layout_gcwx_xd_detail / 100000);
    }

    //
    public void btn5_click(View v) {
        Intent intent = new Intent(getApplicationContext(), Gcwx_foreign.class);
        //传递参数
        Bundle bundle = new Bundle();
        bundle.putSerializable("vouchsforeignlist", vouchsforeignlist);
        bundle.putSerializable("vouch", vouch);
        intent.putExtra("listtype", listtype);
        intent.putExtra("ctype", "xd");
        intent.putExtra("currentrow", 0);
        intent.putExtras(bundle);
        startActivityForResult(intent, R.layout.layout_gcwx_xd_detail / 100000);
    }

    //
    public void btn6_click(View v) {
        Intent intent = new Intent(getApplicationContext(), Gcwx_persons.class);
        //传递参数
        Bundle bundle = new Bundle();
        bundle.putSerializable("vouchspersonslist", vouchspersonslist);
        bundle.putSerializable("vouch", vouch);
        intent.putExtra("listtype", listtype);
        intent.putExtra("ctype", "xd");
        intent.putExtra("currentrow", 0);
        intent.putExtras(bundle);
        startActivityForResult(intent, R.layout.layout_gcwx_xd_detail / 100000);
    }

    //向左翻页
    public void btn_abs_back(View v) {
        if (icurrentrow == 0) {
            AbToastUtil.showToastInThread(getApplicationContext(), "已经是第一项");
        } else {
            insert_data(icurrentrow - 1);
        }
    }

    //向右翻页
    public void btn_abs_forward(View v) {
        if (icurrentrow == (vouchlist.size() - 1)) {
            AbToastUtil.showToastInThread(getApplicationContext(), "已经是最后一项");
        } else {
            insert_data(icurrentrow + 1);
        }
    }

    //接收返回数据(存货明细,行切换,成本对象)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            Bundle bundle = data.getExtras();
            switch (resultCode) {
                case R.layout.layout_gcwx_inventory / 200000:
                    //更新耗用材料
                    vouchslist = (ArrayList<GcwxVouchs>) data.getSerializableExtra("vouchslist");
                    break;
                case R.layout.layout_gcwx_foreign / 300000:
                    //更新外包材料
                    vouchsforeignlist = (ArrayList<GcwxVouchsForeign>) data.getSerializableExtra("vouchsforeignlist");
                    break;
                case R.layout.layout_gcwx_persons / 400000:
                    //更新外包材料
                    vouchspersonslist = (ArrayList<GcwxVouchsPersons>) data.getSerializableExtra("vouchspersonslist");
                    break;
                default:
                    //其它窗口的回传数据
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

